import api from '~api/index'
import util from '~src/lib/util'
const getRefuelingList = 'getRefuelingList'
const setRefuelingList = 'setRefuelingList'
const getRefuelingshowList = 'getRefuelingshowList'
const setRefuelingshowList = 'setRefuelingshowList'
const getRefuelingshow = 'getRefuelingshow'
const setRefuelingShow = 'setRefuelingShow'
const getStatistics = 'getStatistics'
export default {
  state: {
   list: {
      data: [],
      page: 1,
      code:'0',
      id:'-1',
      total:0
    },
    statistics:{
      totalNum:'',
      totalPayPrice:'',
      totalGsPrice:''
    },
    show:{}
  },
  mutations: {
    ['setRefuelingShow'] (state, data) {
      state.show = data
    },
    ['setRefuelingshowList'] (state, data) {
      if (data.code == '200'){
        if (data.page === 1) {
          data.data = [].concat(data.data)
          if (data.data.length === 0) {
            data.data = []
          }
        } else {
          data.data = state.list.data.concat(data.data)
        }
        state.list.data = data.data
        state.list.total = data.total
      }
      state.list.code = data.code
    },
    ['setStatistics'] (state, statistics) {
      state.statistics = {
        totalNum:statistics.totalNum,
        totalPayPrice:statistics.totalPayPrice,
        totalGsPrice:statistics.totalGsPrice
      }
    },
    ['updataRefuelingshow'] (state, id) {
      const data = state.list.data.find(ii => ii.orderNo === id)
      state.show = data
    }
  },
  actions: {
    async ['getRefuelingshowList'] ({commit}, config) {
      const { data: { data, code } } = await api.post('/v1/orderHistory',
        {openid: util.localItem.get('openid'), gsId: config.id, page: config.page, page_size: 11})
      if (data && code === '200') {
        commit('setRefuelingshowList', {
          data: data.history_list.length>0?data.history_list:[],
          page: config.page,
          id: config.id,
          code:code,
          total: data.history_list.length>0?data.history_list.length:0
        })
        commit('setStatistics',data.statistics)
        config.callback && config.callback()
      } else {
        commit('setRefuelingshowList', {
          data: [],
          page: config.page,
          id: config.id,
          total: 0,
          code:code
        })
        config.stopCallback && config.stopCallback()
      }
    },
    async ['getRefuelingshow'] ({commit}, orderNo) {
      const { data: { data, code } } = await api.post('/v1/orderDetail',
        {openid: util.localItem.get('openid'), orderNo: orderNo})
      if (data && code === '200') {
        commit('setRefuelingShow', data)
      }
    }
  },
  getters: {
    ['getRefuelingshowList'] (state) {
      return state.list
    },
    ['getRefuelingshow'] (state) {
      return state.show
    },
    ['getStatistics'] (state) {
      return state.statistics
    }
  }
}
